# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets
from gui.data_analysis.multiple_para_setting import Ui_Dialog
from PyQt5.QtGui import QIcon,QPixmap
from PyQt5.QtCore import pyqtSignal,pyqtSlot
from PyQt5.QtWidgets import QDialog,QMessageBox,QColorDialog,QComboBox,QLabel
from utils.color_util import color
from utils.log_util import loggings as logger


class MultipleParaWindow(QtWidgets.QDialog, Ui_Dialog):
    dialogSignal = pyqtSignal(object)

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        icon = QIcon()
        icon.addPixmap(
            QPixmap("icons/logo.ico"),
            QIcon.Normal,
            QIcon.Off)
        self.setWindowIcon(icon)
        self.add_index = 0

    def set_value_2_widget(self, para_list):
        self.para_list = para_list

    @pyqtSlot()
    def on_pb_add_clicked(self):
        l_para_name = QLabel("参数:")
        cbb_para = QComboBox()
        cbb_para.addItems(self.para_list)
        self.gl_widget.addWidget(l_para_name, self.add_index, 0, 1, 1)
        self.gl_widget.addWidget(cbb_para, self.add_index, 1, 1, 1)
        self.add_index += 1

    def show_color_dialog(self):
        try:
            col = QColorDialog.getColor()
            old_color = col.name()
            new_color = color(old_color.upper())
            if col.isValid():
                self.sender().setStyleSheet('QWidget {background-color:%s}' % col.name())
        except Exception as e:
            logger.error("show_color_dialog," + str(e))

    @pyqtSlot()
    def on_pb_delete_clicked(self):
        try:
            if self.add_index > 0:
                for i in range(1,self.gl_widget.columnCount()+1):
                    self.gl_widget.itemAt(self.gl_widget.count() - i).widget().hide()
                    self.gl_widget.itemAt(self.gl_widget.count() - i).widget().deleteLater()

                self.add_index -= 1
                self.gl_widget.invalidate()
            else:
                self.show_notification("提示", "没有了")
        except Exception as e:
            logger.error("on_pb_delete_clicked," + str(e))

    @pyqtSlot()
    def on_btnEnsure_clicked(self):
        column_count = self.gl_widget.columnCount()
        widget_count = self.gl_widget.count()
        try:
            para_list = []
            for i in range(1,widget_count):
                if self.gl_widget.itemAt(i).widget():
                    if isinstance(self.gl_widget.itemAt(i).widget(), QComboBox):
                        current_text = self.gl_widget.itemAt(i).widget().currentText().strip()
                        if current_text not in para_list:
                            para_list.append(current_text)
                        else:
                            continue
                    # else:
                    #     die_list.append(self.gl_widget.itemAt(i).widget().palette().color(QPalette.Background).name())
            # para_list = self.list_of_groups(die_list, column_count)
            if para_list:
                # self.close()
                self.dialogSignal.emit(para_list)
            else:
                self.show_notification("提示", "查询条件不能为空")
        except Exception as e:
            logger.error("on_btnEnsure_clicked," + str(e))

    def list_of_groups(self, init_list, children_list_len):
        list_of_groups = zip(*(iter(init_list),) * children_list_len)
        end_list = [list(i) for i in list_of_groups]
        count = len(init_list) % children_list_len
        end_list.append(init_list[-count:]) if count != 0 else end_list
        return end_list

    def show_notification(self, title, content):
        QMessageBox.information(self, title, content, QMessageBox.Yes, QMessageBox.Yes)

    @pyqtSlot()
    def on_btnCancel_clicked(self):
        self.close()
